<template>
  <div class="login-container">
    <el-form :model="loginForm" :rules="rules" ref="loginFormRef" class="login-form">
      <el-form-item prop="userName">
        <el-input v-model="loginForm.userName" placeholder="请输入用户名">
          <template #prefix>
            <el-icon><User /></el-icon>
          </template>
        </el-input>
      </el-form-item>
      <el-form-item prop="userPassword">
        <el-input v-model="loginForm.userPassword" type="password" placeholder="请输入密码">
          <template #prefix>
            <el-icon><Lock /></el-icon>
          </template>
        </el-input>
      </el-form-item>
      <div class="button-group">
        <el-button type="primary" @click="handleLogin">登录</el-button>
        <el-button type="danger" @click="handleLogout">退出登录</el-button>
        <el-button type="warning" @click="handleRegister">注册</el-button>
        <el-button type="info" @click="handleGetInfo">获取信息</el-button>
        <el-button type="info" @click="console.log(token)">打印token</el-button>
        <span style="display: block">{{token?"当前已登录,已获取token":"当前未登录,请登录"}}</span>
      </div>
    </el-form>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import { User, Lock } from '@element-plus/icons-vue'
import type { FormInstance, FormRules } from 'element-plus'
import axios from '@/utils/axios.ts'
import cookies  from "@/utils/cookies.ts";

const loginFormRef = ref<FormInstance>()
const isLogin = ref(false)
const token = ref(cookies.get('token'))

const loginForm = reactive({
  userName: '',
  userPassword: ''
})

const rules = reactive<FormRules>({
  userName: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
  ],
  userPassword: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
  ]
})

const handleLogin = async () => {
  console.log(loginFormRef.value)
  if (!loginFormRef.value) return
  await loginFormRef.value.validate((valid) => {
    if (valid) {
      console.log('登录表单', JSON.stringify(loginForm))
      // TODO: 实现登录请求
      axios.post('/login', loginForm)
        .then((res) => {
          if (res.code==2000){
            console.log(res.data.token)
            cookies.set('token', res.data.token,  7 )
            cookies.set('userName', res.data.userName,  7 )
            isLogin.value = true
            token.value=res.data.token
            alert("登录成功")
          }
        })
    }
  })
}

const handleLogout = () => {
  // TODO: 实现退出登录请求
  console.log("开始退出登录")
  axios.post('/logout')
    .then((res) => {
      if (res.code==2000){
        isLogin.value = false
        token.value=''
        cookies.remove('token')
        alert(res.data)
      }
    })
}

const handleRegister = () => {
  // TODO: 实现测试请求
  if (!loginFormRef.value) return
  loginFormRef.value.validate((valid) => {
    if (valid) {
      console.log('注册表单', JSON.stringify(loginForm))
      // TODO: 实现注册请求
      axios.post('/register', JSON.stringify(loginForm))
        .then((res) => {
          if (res.code==2000){
            alert(res.data)
          }
        })
    }
  })

}

const handleGetInfo = () => {
  // TODO: 实现获取信息请求
  console.log('获取信息请求')
  axios.get('/test')
      .then((res) => {
        if (res.code==2000){
          console.log(res.data)
          alert('测试请求成功')
        }
      })
}
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
}

.login-form {
  width: 400px;
  padding: 40px;
  background: white;
  border-radius: 12px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.1);
}

.el-form-item {
  margin-bottom: 25px;
}

.el-input :deep(.el-input__wrapper) {
  padding: 12px;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
  transition: all 0.3s ease;
}

.el-input :deep(.el-input__wrapper):hover {
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}

.el-input :deep(.el-input__prefix) {
  margin-right: 8px;
}


.button-group span {
  flex:  1 0 300px;
}
</style>